import axios from "axios";
import * as actionTypes from "./actionTypes";
import { fromJS } from "immutable";

const changeHomeData = result => ({
    type: actionTypes.CHANGE_HOME_DATA,
    topicList: result.topicList,
    articleList: result.articleList,
    recommendList: result.recommendList
});

/* 获取首页信息 */
export const getHomeInfo = () => dispatch => {
    axios
        .get("./api/homeContent.json")
        .then(res => {
            const result = res.data.data;
            dispatch(changeHomeData(result));
        })
        .catch(err => {
            console.error(err);
        });
};

const addHomeList = (articleList, nextPage) => ({
    type: actionTypes.ADD_ARTICLE_LIST,
    articleList: fromJS(articleList),
    page: nextPage
});

/* 获取更多信息 */
export const getMoreList = page => dispatch => {
    axios
        .get(`./api/homeList.json?page=${page}`)
        .then(res => {
            const result = res.data.data;
            // console.log(result);
            dispatch(addHomeList(result, page + 1));
            // dispatch(changeHomeData(result));
        })
        .catch(err => {
            console.error(err);
        });
};

export const toggleTopShow = show => ({
    type: actionTypes.TOGGLE_SCROLL_TOP,
    show
});
